package com.fruit.service.pay.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.constants.RedisConstant;
import com.fruit.pojo.goods.GoodsSpecTime;
import com.fruit.pojo.pay.PayInfo;
import com.fruit.service.pay.PayHandel;
import com.fruit.service.pay.PayManager;
import com.fruit.service.redis.JedisUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 订单处理逻辑类
 *
 * @author shibeilei
 * @version 2017.11.30 19:17
 *
 */
@Slf4j
@Service
public class PayHandelNothing implements PayHandel {
	
	@Autowired
	private PayManager payService;

	@Autowired
	private JedisUtil jedisService;

	@Override
	public Map<String, Object> saveSubmitOrder(String tradeId, PayInfo pi) {
		log.info("[{}]-【订单提交】-【用户：{}】-【开始】", tradeId, pi.getUserId());
		Map<String, Object> resMap = new HashMap<String, Object>();
		int areaGoodsCountId = 0;
		boolean lock = false;
		try {
			if (!isRepeatSubmit(pi.getSubmitToken(), pi.getUserId())) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "repeatSubmit", "请不要重复提交!"));
				return resMap;
			}
			
			log.info("[{}]-【订单提交】-【校验参数】", tradeId);
			resMap = payService.findCheckOrderParams(tradeId, pi);
			
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (ResultInfo.ERROR.equals(ri.getState())) {
				resMap.put("result", ri);
				return resMap;
			}
			log.info("[{}]-【订单提交】-【清除防重锁】", tradeId);
			jedisService.del(RedisConstant.USER_SUBMIT_TOOKEN + pi.getUserId());

			GoodsSpecTime goodsSpecTime = (GoodsSpecTime) resMap.get("goodsSpecTime");
			areaGoodsCountId = goodsSpecTime.getAreaGoodsCountId();
			log.info("[{}]-【订单提交】-【获取锁】", tradeId);
			lock = jedisService.lock(RedisConstant.AREA_GOODS_COUNT_LOCK + areaGoodsCountId, 40000, 15);
			if (!lock) {
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "get_lock_error", "请重新结算!"));
				return resMap;
			} else {
				log.info("[{}]-【订单提交】-【保存订单数据】", tradeId);
				resMap = payService.saveSubmitOrder(goodsSpecTime, pi);
				return resMap;
			}

		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result",
					ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!", null, ResultInfo.ROLLBACK));
			return resMap;
		} finally {
			if (lock) {
				jedisService.unlock(RedisConstant.AREA_GOODS_COUNT_LOCK + areaGoodsCountId);
			}
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (ResultInfo.ERROR.equals(ri.getState())) {
				log.error("[{}]-【订单提交】-【异常】-【{}：{}】", tradeId, ri.getCode(), ri.getMessage());
			} else {
				log.info("[{}]-【订单提交】-【成功】", tradeId);
			}
			log.info("[{}]-【订单提交】-【结束】", tradeId);
		}
	}

	private boolean isRepeatSubmit(String token, int userId) {
		log.info("【提交TOKEN】:{}", token);
		if (StringUtils.isEmpty(token)) {
			return false;
		}
		String serverToken = jedisService.get(RedisConstant.USER_SUBMIT_TOOKEN + userId);
		log.info("【缓存TOKEN】:{}", serverToken);
		if (StringUtils.isEmpty(serverToken)) {
			return false;
		}
		if (!token.equals(serverToken)) {
			return false;
		}
		return true;
	}

}
